import random

from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views import View

from chat.models import Room
from record.models import Record
from tools.login_dec import login_check
import json
import html


# Create your views here.


class RecordView(View):
    # 聊天列表的返回值函数
    def get(self, request, room_id):
        records = Record.objects.filter(room_id=room_id)
        records_res = []
        for record in records:
            records_res.append({
                'id': record.id,
                'time': record.created_time.strftime('%Y-%m-%d %H:%M:%S'),
                'nick': record.user.nickname,
                'avatar': str(record.user.avatar),
                'sex': random.randint(0, 2),
                'words': record.content,
                'imgs': [],
            })
        # 返回值
        result = {'code': 200, 'data': records_res}
        return JsonResponse(result)

    # # 聊天详情页的返回值方法
    # def make_record_res(self,user, user_record, is_self):
    #     result = {'code': 200, 'data': {}}
    #     # 1.聊天数据
    #     result['data']['content'] = user_record.content
    #     result['data']['user'] = user.username
    #     result['data']['created_time'] = user_record.created_time.strftime('%Y-%m-%d %H:%M:%S')
    #     result['data']['room']=user_record.room_name
    #     # 3. 聊天数据
    #     all_record = Record.objects.filter(record=user_record).order_by('-created_time')
    #     # 统计聊天记录的数量
    #     red_count = 0
    #     for red in all_record:
    #        red_count+=1#总聊天数据
    #
    #     return red_count
